﻿using Helper; 
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Web.Http;

namespace Langchur.Controllers
{
    public class CommonDate : AuthBase
    {
        public String Name
        {
            get;
            set;
        }

        public String Parameters
        {
            get;
            set;
        } 
    }

    public class DataManagerController : BaseController
    {
        //[HttpPost]
        //public HttpResponseMessage Select(CommonDate CD)
        //{
        //    DataManager dm = new DataManager();
        //    DataTable dt = dm.Select(CD.Name, CD.Parameters).Tables[0];
        //    return CResponse.ReturnSuccess(JSONManager.DataTableToJson_v2(dt));
        //}

        //[HttpPost]
        //public HttpResponseMessage Delete(CommonDate CD)
        //{
        //    DataManager dm = new DataManager();

        //    return CResponse.ReturnSuccess("{\"status\":" + dm.Delete(CD.Name, CD.ID).ToString().ToLower() + "}");
        //}

        //[HttpPost]
        //public HttpResponseMessage Update(CommonDate CD)
        //{
        //    DataManager dm = new DataManager();
        //    return CResponse.ReturnSuccess("{\"status\":" + dm.Update(CD.Name, CD.ID.ToString(), CD.Parameters).ToString().ToLower() + "}");
        //}

        //[HttpPost]
        //public HttpResponseMessage Insert(CommonDate CD)
        //{
        //    DataManager dm = new DataManager();
        //    return CResponse.ReturnSuccess("{\"id\":" + dm.Insert(CD.Name, CD.Parameters).ToString() + "}");
        //} 

        [HttpGet]
        public HttpResponseMessage TestGet(Int32 i)
        {
            return CResponse.ReturnSuccess("{\"table\":[{\"ID\":" + i + "}]}");
        }

        [HttpPost]
        public HttpResponseMessage TestPost(CommonDate CD)
        {
            return CResponse.ReturnSuccess(Newtonsoft.Json.JsonConvert.SerializeObject(CD));
        }

        [HttpPost]
        public HttpResponseMessage Proc(CommonDate CD)
        {  
            if (CD.Name == "T56_SSOLogin" || CD.Name == "T56_SSOLoginById")
            {
                CD.Auth = true;
            }
            if (!CD.Auth)
            {
                return CResponse.ReturnSuccess("{\"table\":[{\"ErrorVersion\":\"1\"}]}");
            }

            DataManager dm = new DataManager(new DataLog(this.Url.Request.RequestUri.ToString(), CD.OpUid));


            //dm.Procedure("ProcLog", "[{\"Name\":\"" + CD.Name + "\",\"Parm\":\"" + CD.Parameters + "\"}]");

            DataTable dt = new DataTable();
            try
            {
                //try
                //{
                //    TrackHandle.SaveTrack(this.Url.Request.RequestUri.ToString(), CD.Name, CD.Parameters);
                //}
                //catch (Exception ex)
                //{
                //ErrorHandle.SaveError(this.Url.Request.RequestUri.ToString(), Newtonsoft.Json.JsonConvert.SerializeObject(CD), ex.Message);
                //}

                //TrackHandle.SaveTrackForce("proc", CD.Name, CD.Parameters);
                dt = dm.Procedure(CD.Name, CD.Parameters);

                if(CD.Name == "ClientClassCourseManager")
                {
                    //发送邮件
                }
            }
            catch (Exception ex)
            {
                //String Parms = "{\"Name\":\"" + CD.Name + "\",\"Parameters\":\"" + CD.Parameters + "\",\"AuthCode\":\"" + CD.AuthCode + "\"}"; 
                ErrorHandle.SaveError(this.Url.Request.RequestUri.ToString(), Newtonsoft.Json.JsonConvert.SerializeObject(CD), ex.ToString());
                throw new Exception(ex.ToString());
            }
            return CResponse.ReturnSuccess(JSONManager.DataTableToJson_v2(dt));
        }
         

        [HttpPost]
        public HttpResponseMessage ProcAddSet(CommonDate CD)
        {
             
            DataManager dm = new DataManager(new DataLog(this.Url.Request.RequestUri.ToString(), CD.OpUid));
            //dm.Procedure("ProcLog", "[{\"Name\":\"" + CD.Name + "\",\"Parm\":\"" + CD.Parameters + "\"}]");


            List<String> res_list = new List<String>();
            String Result = "";
            try
            {
                List<Dictionary<String, String>> JSONList = JSONManager.ListJsonParse(CD.Parameters);

                for (Int32 i = 0; i < JSONList.Count; i++)
                {


                    res_list.Add(JSONManager.DataSetToJson_v2(dm.ProcedureSet(CD.Name,
                        "[" + Newtonsoft.Json.JsonConvert.SerializeObject(JSONList[i]) + "]"
                        )));
                }

                
                foreach (String line in res_list)
                {
                    Result += line + ",";
                }
                if (Result.Length > 0)
                {
                    Result = Result.Substring(0, Result.Length - 1);
                }

                Result = "[" + Result + "]";
            }  
            catch (Exception ex)
            {
                //String Parms = "{\"Name\":\"" + CD.Name + "\",\"Parameters\":\"" + CD.Parameters + "\",\"AuthCode\":\"" + CD.AuthCode + "\"}"; 
                ErrorHandle.SaveError(this.Url.Request.RequestUri.ToString(), Newtonsoft.Json.JsonConvert.SerializeObject(CD), ex.Message);
                throw new Exception(ex.ToString());
            }
            if(Result == "")
            {
                Result = "[]";
            }
            return CResponse.ReturnSuccess(Result);
        }
         

        [HttpPost]
        public HttpResponseMessage ProcSet(CommonDate CD)
        {
            DataManager dm = new DataManager(new DataLog(this.Url.Request.RequestUri.ToString(), CD.OpUid));
            DataSet ds = new DataSet(); 

            try
            {
                ds = dm.ProcedureSet(CD.Name, CD.Parameters);
            }
            catch (Exception ex)
            {
                //String Parms = "{\"Name\":\"" + CD.Name + "\",\"Parameters\":\"" + CD.Parameters + "\",\"AuthCode\":\"" + CD.AuthCode + "\"}"; 
                ErrorHandle.SaveError(this.Url.Request.RequestUri.ToString(), Newtonsoft.Json.JsonConvert.SerializeObject(CD), ex.Message);
                throw new Exception(ex.ToString());
            }
            return CResponse.ReturnSuccess(JSONManager.DataSetToJson_v2(ds));
        }
    }
}
